package com.laf.controller;

import com.laf.model.Userinfo;
import com.laf.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author: He Changjie  on  2018-09-28
 * @description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    /**
     * 验证用户名
     * @param userinfo 用户名
     * @return
     */
    @RequestMapping("/findOne")
    public String findOne(Userinfo userinfo, HttpServletRequest request){
        Userinfo byUserNmae = userService.findByUserName(userinfo);
        if(byUserNmae!=null){
            request.setAttribute("findUser",byUserNmae);
            return "forward:/views/laf-findTwo.jsp";
        }else{
            request.setAttribute("status",false);
            request.setAttribute("inputUserName",userinfo.getUserName());
            return "forward:/views/laf-findOne.jsp";
        }
    }

    @RequestMapping("/findTwo")
    public String findTwo(Userinfo userinfo,HttpServletRequest request){
        Userinfo user=userService.findByUserName(userinfo);
        if(user.getAnswer().equals(userinfo.getAnswer())){
            request.setAttribute("userName",userinfo.getUserName());
            return "forward:/views/laf-findThree.jsp";
        }else{
            request.setAttribute("status",false);
            request.setAttribute("inputUserName",userinfo.getUserName());
            request.setAttribute("inputQue",user.getQuestion().getTitle());
            request.setAttribute("inputAnswer",userinfo.getAnswer());
            return "forward:/views/laf-findTwo.jsp";
        }
    }

    @RequestMapping("/findThree")
    public String findThree(Userinfo userinfo){
        Userinfo user= userService.findByUserName(userinfo);
        user.setPassword(userinfo.getPassword());
        return "redirect:/views/laf-login.jsp";
    }

    @RequestMapping("/register")
    public String register(Userinfo userinfo,String code,HttpSession session,ModelMap modelMap){
        String vcode = (String) session.getAttribute("vcode");
        if(!vcode.equalsIgnoreCase(code)){
            modelMap.addAttribute(userinfo);
            return "forward:/views/laf-register.jsp?codeError=error";
        }
        try {
            userService.saveUser(userinfo);
        } catch (Exception e) {
            e.printStackTrace();
            return "forward:/views/laf-register.jsp?status=error";
        }
        return "redirect:/laf/showLogin";
    }

    @RequestMapping("/login")
    public String login(Userinfo userinfo,String code, HttpSession session){
        String vcode = (String) session.getAttribute("vcode");
        if(!(vcode != null && vcode.equalsIgnoreCase(code))){
            return "forward:/views/laf-login.jsp?userName="+userinfo.getUserName()+"&status=error";
        }
        Userinfo user=userService.login(userinfo);
        if(user!=null){
            session.setAttribute("logUser",user);
            userService.updateLogTime(user);
            return "redirect:/index.jsp";
        }else{
            return "forward:/views/laf-login.jsp?userName="+userinfo.getUserName()+"&logInfo=error";
        }
    }

    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/laf/showIndex";
    }
}
